package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.bean.TiXianDetailBean;
import com.jdew.thkj.bean.ZhaoshangBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.nio.DoubleBuffer;
import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class TixianAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    public List<TiXianDetailBean.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private OnItemClickListener mItemClickListener;
    private String maType;
    public static final int LOADING_MORE = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE = 2;

    public static final int PULL_LOAD = 3;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public TixianAdapter(Context context, List<TiXianDetailBean.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
        sp = context.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        maType = sp.getString("SpareTwo", "");
    }

    public void setDataList(List<TiXianDetailBean.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }
    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            itemView = layoutInflater.from(context).inflate(R.layout.tixian_item, parent, false);
            return new MyViewHolder(itemView);
        } else if (viewType == TYPE_FOOTER) {
            itemView = layoutInflater.from(context).inflate(R.layout.item_footer, parent,
                    false);
            return new FootViewHolder(itemView);
        }
        return new MyViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myviewHolder = (MyViewHolder) holder;
            myviewHolder.BindView(holder, position);
            myviewHolder.itemView.setTag(position);
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footerViewHolder = (FootViewHolder) holder;
            switch (mLoadMoreStatus) {
                case LOADING_MORE:
                    footerViewHolder.mTvLoadText.setText("正加载更多...");
                    break;
                case PULL_LOAD:
                    footerViewHolder.progressBar.setVisibility(View.GONE);
                    footerViewHolder.mTvLoadText.setText("上拉加载更多");
                    break;
                case NO_LOAD_MORE:
                    footerViewHolder.mTvLoadText.setText("已全部加载");
                    footerViewHolder.progressBar.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size() + 1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.money)
        TextView money;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.state)
        TextView state;

        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
            DecimalFormat df = new DecimalFormat("0.00");
            ((MyViewHolder) holder).money.setText(df.format(Double.parseDouble(dataList.get(position).getUserMoeny())));
            ((MyViewHolder) holder).time.setText(dataList.get(position).getBeginTime());
            if(dataList.get(position).getUserState().equals("0")){
                ((MyViewHolder) holder).state.setText("提现中");
                ((MyViewHolder) holder).state.setTextColor(context.getResources().getColor(R.color.theme_color));
            }else if(dataList.get(position).getUserState().equals("1")){
                ((MyViewHolder) holder).state.setText("支付宝已转账");
                ((MyViewHolder) holder).state.setTextColor(context.getResources().getColor(R.color.top_title));
            }else if(dataList.get(position).getUserState().equals("2")){
                ((MyViewHolder) holder).state.setText("银行卡已转账");
                ((MyViewHolder) holder).state.setTextColor(context.getResources().getColor(R.color.top_title));
            }else if(dataList.get(position).getUserState().equals("-1")){
                ((MyViewHolder) holder).state.setText("已驳回"+"（"+dataList.get(position).getRejectionReason()+")");
                ((MyViewHolder) holder).state.setTextColor(context.getResources().getColor(R.color.red));
            }
        }
    }

    public static class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.loading)
        TextView mTvLoadText;
        @Bind(R.id.progressBar)
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 更新加载更多状态
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }

}
